//
//  MYGChatTopView.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/4/22.
//

import UIKit
import SwiftDate
import SVGKit

class MYGChatTopView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
        backgroundColor = .white
    }
    
    // MARK: - Property
    var model: MYGChatListCellModel? {
        didSet {
            guard let model = model else {
                return
            }
            
//            if model.isNetwork {
//                if model.image.contains("http") {
//                    avatar.sd_setImage(with: URL(string: model.image))
//                } else {
//                    if let data = UserDefaults.standard.data(forKey: model.image) {
//                        guard let svgImage = SVGKImage(data: data) else {
//                            return
//                        }
//                        
//                        self.avatar.image = svgImage.uiImage
//                    } else {
//                        DispatchQueue.global().async {
//                            let url = URL(string: model.image)!
//                            guard let data = try? Data(contentsOf: url) else {
//                                return
//                            }
//                            
//                            UserDefaults.standard.setValue(data, forKey: model.image)
//                            UserDefaults.standard.synchronize()
//                            guard let svgImage = SVGKImage(data: data) else {
//                                return
//                            }
//                    
//                            DispatchQueue.main.async {
//                                self.avatar.image = svgImage.uiImage
//                            }
//                        }
//                    }
//                }
//            } else {
                avatar.image = UIImage(named: "Chat/\(model.image)")
//            }
            
            nameLabel.text = model.name
            contentLabel.text = model.content
            
            timeLabel.text = model.time
            
            redImageView.isHidden = !model.haveNew
            lineView.isHidden = !model.showLine
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        addSubview(avatar)
        addSubview(redImageView)
        addSubview(stackView)
        addSubview(timeLabel)
        addSubview(lineView)
        
        stackView.addArrangedSubviews([nameLabel, contentLabel])
        
        avatar.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(40)
        }
        
        redImageView.snp.makeConstraints { make in
            make.right.equalTo(avatar.snp.right)
            make.top.equalTo(avatar.snp.top).offset(-3)
        }
        
        stackView.snp.makeConstraints { make in
            make.left.equalTo(avatar.snp.right).offset(15)
            make.right.equalTo(timeLabel.snp.left).offset(-20)
            make.centerY.equalToSuperview()
        }
        
        timeLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(15)
            make.centerY.equalTo(nameLabel)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        timeLabel.setContentHuggingPriority(.required, for: .horizontal)
        timeLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
    }
    // MARK: - lazy load
    private lazy var redImageView: UIImageView = {
        let imgV = UIImageView(image: UIImage(named: "Chat/red"))
        return imgV
    }()
    
    private lazy var avatar: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 20
        return imgV
    }()
    
    private lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .vertical
        stackV.spacing = 2
        return stackV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(16)
        label.textColor = .rgba(44, 44, 44, 1)
        return label
    }()
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(13)
        label.textColor = .rgba(128, 128, 128, 1)
        return label
    }()
    
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(13)
        label.textColor = .rgba(128, 128, 128, 1)
        return label
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238, 1)
        return view
    }()
    
    
    
    
}
